package cn.tedu.boot02.controller;

import cn.tedu.boot02.entity.User;
import cn.tedu.boot02.util.DBUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Controller
public class UserController {
    @ResponseBody
    @RequestMapping("/login")
    public String login(User user, HttpServletResponse response){
        try(Connection conn= DBUtil.getConnection()){
            String sql="select password from user where username=?";
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1,user.getName());
            ResultSet rs=ps.executeQuery();
            String pwd="";
            if (rs.next()){
               pwd=rs.getString("password");
               if(pwd.equals(user.getPassword())){
                   return "登录成功";
               }
               return "密码错误";
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        System.out.println("user = " + user);
        return "登录失败用户名不存在";
    }
}
